package com.lmpop.trade.ti_admin;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.MD5;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Ti_adminInfo implements TradeInterf
{
  QueryList queryList;
  public Ti_adminInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(map.getString("user_id"));
    ti_admin.setUser_name(map.getString("user_name"));

    MD5 md5 = new MD5();
    String passwd = map.getString("passwd");
    String passwdmd5 = md5.getMD5(passwd.getBytes());
    ti_admin.setPasswd(passwdmd5);

    ti_admin.setCust_id(map.getString("cust_id"));
    ti_admin.setReal_name(map.getString("real_name"));
    ti_admin.setPhone(map.getString("phone"));
    ti_admin.setEmail(map.getString("email"));
    ti_admin.setCellphone(map.getString("cellphone"));
    ti_admin.setFax(map.getString("fax"));
    ti_admin.setQq(map.getString("qq"));
    ti_admin.setMsn(map.getString("msn"));

    if ((map.getString("u_phone") != null) && (!map.getString("u_phone").equals("")))
      ti_admin.setPhone(map.getString("u_phone"));
    if ((map.getString("u_email") != null) && (!map.getString("u_email").equals("")))
      ti_admin.setEmail(map.getString("u_email"));
    if ((map.getString("u_cellphone") != null) && (!map.getString("u_cellphone").equals("")))
      ti_admin.setCellphone(map.getString("u_cellphone"));
    if ((map.getString("u_fax") != null) && (!map.getString("u_fax").equals("")))
      ti_admin.setFax(map.getString("u_fax"));
    if ((map.getString("u_qq") != null) && (!map.getString("u_qq").equals("")))
      ti_admin.setQq(map.getString("u_qq"));
    if ((map.getString("u_msn") != null) && (!map.getString("u_msn").equals(""))) {
      ti_admin.setMsn(map.getString("u_msn"));
    }
    ti_admin.setUser_state(map.getString("user_state"));
    ti_admin.setUser_type(map.getString("user_type"));
    ti_admin.setOrg_id(map.getString("org_id"));
    ti_admin.setRole_code(map.getString("role_code"));
    ti_admin.setPasswd_ques(map.getString("passwd_ques"));
    ti_admin.setPasswd_answer(map.getString("passwd_answer"));
    ti_admin.setStaff_id(map.getString("session_user_id"));
    ti_admin.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      insert(dbmMgr, ti_admin);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_admin ti_admin)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_admin", ti_admin);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTi_admin", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(map.getString("user_id"));
    ti_admin.setCust_id(map.getString("cust_id"));
    ti_admin.setReal_name(map.getString("real_name"));
    ti_admin.setPhone(map.getString("phone"));
    ti_admin.setEmail(map.getString("email"));
    ti_admin.setCellphone(map.getString("cellphone"));
    ti_admin.setFax(map.getString("fax"));
    ti_admin.setQq(map.getString("qq"));
    ti_admin.setMsn(map.getString("msn"));
    ti_admin.setOrg_id(map.getString("org_id"));
    ti_admin.setRole_code(map.getString("role_code"));
    ti_admin.setPasswd_ques(map.getString("passwd_ques"));
    ti_admin.setPasswd_answer(map.getString("passwd_answer"));
    ti_admin.setStaff_id(map.getString("session_user_id"));
    ti_admin.setRemark(map.getString("remark"));

    String result_code = "0";
    try {
      update(dbmMgr, ti_admin);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_admin ti_admin)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_admin", ti_admin);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ti_admin ti_admin = (Ti_admin)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }

    ti_admin.setStart(Integer.valueOf(start));
    ti_admin.setLimit(Integer.valueOf(limit));

    return this.queryList.getList("getTi_adminList", ti_admin);
  }

  public int getCountByObj(Object obj)
  {
    Ti_admin ti_admin = (Ti_admin)obj;
    List list = this.queryList.getList("getTi_adminCount", ti_admin);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_adminByPk", pk);
  }

  public List checkUser(String user_name, String passwd)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_name(user_name);
    ti_admin.setPasswd(passwd);
    return this.queryList.getList("getTi_adminByUserPasswd", ti_admin);
  }

  public SysMap checkLogin(DbmInterf dbmMgr, SysMap map)
  {
    String user_name = map.getString("user_name");
    String passwd = map.getString("passwd");
    String userrand = map.getString("userrand");
    String randcode = map.getString("randcode");

    String result_code = "0";
    MD5 md5 = new MD5();
    String passwdmd5 = md5.getMD5(passwd.getBytes());
    String result_info = "";
    List userList = checkUser(user_name, passwdmd5);

    if ((userList != null) && (userList.size() > 0)) {
      if (userrand.trim().equals(randcode.trim())) {
        Hashtable userMap = (Hashtable)userList.get(0);
        String user_id = ""; String cust_id = ""; String user_state = ""; String user_type = ""; String role_code = ""; String cust_class = ""; String area_attr = "";
        if (userMap.get("user_id") != null) user_id = userMap.get("user_id").toString();
        if (userMap.get("cust_id") != null) cust_id = userMap.get("cust_id").toString();
        if (userMap.get("user_state") != null) user_state = userMap.get("user_state").toString();
        if (userMap.get("user_type") != null) user_type = userMap.get("user_type").toString();
        if (userMap.get("role_code") != null) role_code = userMap.get("role_code").toString();
        if (userMap.get("area_attr") != null) area_attr = userMap.get("area_attr").toString();
        if ((user_type.equals("0")) || (user_type.equals("1"))) {
          if (user_state.equals("1")) {
            result_info = "�Բ��𣬸��û��ѽ��ã��������¼!";
            result_code = "1";
          } else {
            map.setSessionField("session_user_id", user_id);
            map.setSessionField("session_area_attr", area_attr);
            map.setSessionField("session_cust_id", cust_id);
            map.setSessionField("session_user_type", user_type);
            map.setSessionField("session_role_code", role_code);
            map.setSessionField("session_user_name", user_name);

            map.setString("session_user_id", user_id);
            map.setString("session_area_attr", area_attr);
            map.setString("session_cust_id", cust_id);
            map.setString("session_user_type", user_type);
            map.setString("session_role_code", role_code);
            map.setString("session_user_name", user_name);
          }
        } else {
          result_info = "�Բ������ǹ���Ա���������¼!";
          result_code = "1";
        }
      } else {
        result_info = "��֤�����벻��ȷ!";
        result_code = "1";
      }
    } else {
      result_info = "�û�����ڻ��������!";
      result_code = "1";
    }
    map.setString("result_code", result_code);
    map.setString("result_info", result_info);
    return map;
  }

  public String getUserNameByPK(String pk)
  {
    List user_list = this.queryList.getList("getUser_NameByPK", pk);
    String user_name = "";
    Hashtable userMap = new Hashtable();
    if ((user_list != null) && (user_list.size() > 0)) userMap = (Hashtable)user_list.get(0);
    if (userMap.get("user_name") != null) user_name = userMap.get("user_name").toString();
    return user_name;
  }

  public int checkRole(String role_code)
  {
    List user_list = this.queryList.getList("getTi_adminByRole", role_code);
    if ((user_list != null) && (user_list.size() > 0)) {
      return 1;
    }
    return 0;
  }

  public int checkUserName(String user_name)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_name(user_name);
    List user_list = this.queryList.getList("check_UserName", ti_admin);
    if ((user_list != null) && (user_list.size() > 0)) {
      return 1;
    }
    return 0;
  }

  public SysMap updateState(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String user_state = map.getString("user_state");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++)
          if (!pkStr[i].equals(""))
            updateState(dbmMgr, pkStr[i], user_state);
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateState(DbmInterf dbmMgr, String pkid, String user_state)
    throws ApplicationException
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(pkid);
    ti_admin.setUser_state(user_state);
    dbmMgr.update("updateTi_adminState", ti_admin);
  }

  public SysMap updatePasswd(DbmInterf dbmMgr, SysMap map)
  {
    String user_id = map.getString("user_id");

    MD5 md5 = new MD5();
    String passwd = map.getString("passwd");
    String passwdmd5 = md5.getMD5(passwd.getBytes());

    String result_code = "0";
    try {
      updatePasswd(dbmMgr, user_id, passwdmd5);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatePasswd(DbmInterf dbmMgr, String user_id, String passwd)
    throws ApplicationException
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(user_id);
    ti_admin.setPasswd(passwd);
    dbmMgr.update("updateTi_adminPasswd", ti_admin);
  }

  public SysMap updateAdminBasic(DbmInterf dbmMgr, SysMap map)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(map.getString("user_id"));
    ti_admin.setCust_id(map.getString("cust_id"));
    ti_admin.setReal_name(map.getString("real_name"));
    ti_admin.setEmail(map.getString("email"));
    ti_admin.setMsn(map.getString("msn"));
    ti_admin.setQq(map.getString("qq"));
    ti_admin.setCellphone(map.getString("cellphone"));
    ti_admin.setFax(map.getString("fax"));
    ti_admin.setPhone(map.getString("phone"));
    ti_admin.setRemark(map.getString("remark"));

    String result_code = "0";
    try
    {
      updateBasic(dbmMgr, ti_admin);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateBasic(DbmInterf dbmMgr, Ti_admin ti_admin)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_adminBasic", ti_admin);
  }

  public int checkOldPW(String user_id, String old_passwd)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(user_id);

    MD5 md5 = new MD5();
    String passwd = md5.getMD5(old_passwd.getBytes());
    ti_admin.setPasswd(passwd);

    List pw_list = this.queryList.getList("check_OldPW", ti_admin);

    if ((pw_list != null) && (pw_list.size() > 0))
    {
      return 1;
    }

    return 0;
  }

  public SysMap updateAdminPW(DbmInterf dbmMgr, SysMap map)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(map.getString("user_id"));
    ti_admin.setCust_id(map.getString("cust_id"));

    MD5 md5 = new MD5();
    String passwd = map.getString("new_passwd");
    String passwdmd5 = md5.getMD5(passwd.getBytes());
    ti_admin.setPasswd(passwdmd5);

    String result_code = "0";
    try {
      updatePW(dbmMgr, ti_admin);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatePW(DbmInterf dbmMgr, Ti_admin ti_admin)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_adminPW", ti_admin);
  }

  public SysMap updateAdminPWQ(DbmInterf dbmMgr, SysMap map)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(map.getString("user_id"));
    ti_admin.setCust_id(map.getString("cust_id"));
    ti_admin.setPasswd_ques(map.getString("passwd_ques"));
    ti_admin.setPasswd_answer(map.getString("passwd_answer"));

    String result_code = "0";
    try {
      updatePWQ(dbmMgr, ti_admin);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatePWQ(DbmInterf dbmMgr, Ti_admin ti_admin)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_adminPWQ", ti_admin);
  }

  public int checkPasswdAnswer(String user_id, String passwd_answer)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(user_id);
    ti_admin.setPasswd_answer(passwd_answer);

    List pw_list = this.queryList.getList("check_PasswdAnswer", ti_admin);
    if ((pw_list != null) && (pw_list.size() > 0))
    {
      return 1;
    }

    return 0;
  }

  public String getAdminToSelect(String type, String cust_id, String cust_name)
  {
    String selectItems = "<select name=\"" + type + "_select1\" id=\"" + type + "_select1\" size=\"10\" style=\"width:90%\" multiple=\"true\">";
    Ti_admin ti_admin = new Ti_admin();
    if (!cust_name.equals("")) ti_admin.setCust_name(cust_name);
    if (!cust_id.equals("")) ti_admin.setCust_id(cust_id);

    List list = this.queryList.getList("getAdminToSelect", ti_admin);
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        String goodsId = ""; String s_cust_name = "";
        if (map.get("cust_id") != null) {
          goodsId = map.get("cust_id").toString();
        }
        if (map.get("cust_name") != null) {
          s_cust_name = map.get("cust_name").toString();
        }
        selectItems = selectItems + "<option value='" + goodsId + "'>" + s_cust_name + "</option>";
      }
    }

    selectItems = selectItems + "</select>";
    return selectItems;
  }

  public String getcompanyToSelect()
  {
    Ti_admin ti_admin = new Ti_admin();
    List list = this.queryList.getList("getcompanyToSelect", ti_admin);
    String selectItems = "";
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++) {
        Hashtable map = (Hashtable)list.get(i);
        String goodsId = ""; String s_cust_name = "";
        if (map.get("cust_id") != null) {
          goodsId = map.get("cust_id").toString();
        }
        if (map.get("cust_name") != null) {
          s_cust_name = map.get("cust_name").toString();
        }
        selectItems = selectItems + "<option value='" + goodsId + "'>" + s_cust_name + "</option>";
      }
    }

    selectItems = selectItems + "<option value='100000000000000'>��Ӫ��</option>";

    return selectItems;
  }

  public String getAll_User(String cust_id)
  {
    Map ti_admin = new Hashtable();
    if (cust_id != null) {
      ti_admin.put("cust_id", cust_id);
    }
    List userIDLIst = this.queryList.getList("getAll_UserByCustID", ti_admin);
    String selectItems = "";
    if ((userIDLIst != null) && (userIDLIst.size() > 0)) {
      for (int i = 0; i < userIDLIst.size(); i++) {
        Hashtable map = (Hashtable)userIDLIst.get(i);
        String user_id = ""; String user_name = "";
        if (map.get("user_id") != null) {
          user_id = map.get("user_id").toString();
        }
        if (map.get("user_name") != null) {
          user_name = map.get("user_name").toString();
        }
        selectItems = selectItems + "<option value='" + user_id + "'>" + user_name + "</option>";
      }
    }

    return selectItems;
  }

  public String getCustClassByUserID(String user_id)
  {
    Map ti_admin = new Hashtable();
    if (user_id != null) {
      ti_admin.put("user_id", user_id);
    }
    //select a.user_id,a.cust_id,a.user_state,a.user_type,a.role_code,l.cust_class from ti_admin a,ti_custlevel l where a.cust_id = l.cust_id and a.user_id = #user_id#
    List userIDLIst = this.queryList.getList("getCustClassByUserID", ti_admin);
    String cust_class = "";
    if ((userIDLIst != null) && (userIDLIst.size() > 0)) {
      for (int i = 0; i < userIDLIst.size(); i++) {
        Hashtable map = (Hashtable)userIDLIst.get(i);
        if (map.get("cust_class") != null) {
          cust_class = map.get("cust_class").toString();
        }
      }
    }

    return cust_class;
  }

  public String getusernameByEmail(String email)
  {
    String user_name = "";
    List namelist = this.queryList.getList("getusernameByEmail", email);
    if ((namelist != null) && (namelist.size() > 0)) {
      Hashtable map = (Hashtable)namelist.get(0);
      if (map.get("user_name") != null) {
        user_name = map.get("user_name").toString();
      }
    }
    return user_name;
  }

  public List getinfoBynameAndemail(String user_name, String email)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_name(user_name);
    ti_admin.setEmail(email);
    return this.queryList.getList("getinfoBynameAndemail", ti_admin);
  }

  public SysMap updatePersonlPW(DbmInterf dbmMgr, SysMap map)
  {
    Ti_admin ti_admin = new Ti_admin();
    ti_admin.setUser_id(map.getString("user_id"));

    MD5 md5 = new MD5();
    String passwd = map.getString("passwd");
    String passwdmd5 = md5.getMD5(passwd.getBytes());
    ti_admin.setPasswd(passwdmd5);

    String result_code = "0";
    try {
      updateperpw(dbmMgr, ti_admin);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updateperpw(DbmInterf dbmMgr, Ti_admin ti_admin)
    throws ApplicationException
  {
    dbmMgr.update("updateperpw", ti_admin);
  }

  public String getrealNameByPK(String pk)
  {
    List user_list = this.queryList.getList("getUser_NameByPK", pk);
    String user_name = "";
    Hashtable userMap = new Hashtable();
    if ((user_list != null) && (user_list.size() > 0)) userMap = (Hashtable)user_list.get(0);
    if (userMap.get("real_name") != null) user_name = userMap.get("real_name").toString();
    return user_name;
  }
}